package com.nep.service.impl;

import com.nep.entity.Admin;
import com.nep.io.FileIO;
import com.nep.service.AdminService;

import java.util.ArrayList;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    @Override
    public boolean login(String loginCode, String password) {
        try {
            // 读取管理员数据
            Object data = FileIO.readObject("admin.txt");
            if (data == null) {
                return false;
            }

            // 如果数据是字符串数组，转换为 Admin 对象列表
            if (data instanceof String[]) {
                String[] adminArray = (String[]) data;
                for (String adminStr : adminArray) {
                    String[] parts = adminStr.split(",");
                    if (parts.length >= 2 && parts[0].equals(loginCode) && parts[1].equals(password)) {
                        return true;
                    }
                }
            } else if (data instanceof List) {
                // 如果已经是对象列表，尝试转换
                List<?> list = (List<?>) data;
                for (Object obj : list) {
                    if (obj instanceof Admin) {
                        Admin admin = (Admin) obj;
                        if (admin.getLoginCode().equals(loginCode) && admin.getPassword().equals(password)) {
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
